﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP
{
    public partial class PS_AbsenceRules : System.Web.UI.Page
    { 
        private IEmployeeBLL employeeBLL = new EmployeeBLL();
        private IEmployeeTypeBLL employeeTypeBLL = new EmployeeTypeBLL();
        private IAbsenceRuleBLL ruleBLL = new AbsenceRuleBLL(); 
        private KPIS.GERP.GHRMS.APP.Classes.Main main = new KPIS.GERP.GHRMS.APP.Classes.Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                LoadEmpTypeCmb();
                LoadDataGrid(Request["empTypeSeq"]);
            }
        }

        public void LoadEmpTypeCmb()
        {
            var ds = employeeTypeBLL.Select2();
            Dictionary<int, string> dd = new Dictionary<int, string>();
            dd.Add(0, "ทั้งหมด");
            foreach(var d in ds.ToDictionary(d => d.Sequence, d => d.Name))
            {
                dd.Add(d.Key, d.Value);
            }
            radCmbEmpType.DataSource = dd;
            radCmbEmpType.DataBind();
            if(Request["empTypeSeq"] != null &&
                Request["empTypeSeq"] != "" && 
                Request["empTypeSeq"] != "0" &&
                (from d in dd where d.Key.ToString()== Request["empTypeSeq"] select d.Key) != null)
            {
                radCmbEmpType.SelectedValue = Request["empTypeSeq"];
            } else
            {
                radCmbEmpType.SelectedIndex = 0;
            }
        }

        public void LoadDataGrid(string employeeType = "")
        {
            string empTypeForQuery = !string.IsNullOrEmpty(employeeType) && employeeType != "0"
                                         ? employeeType
                                         : "";
            IList<AbsenceRuleInfo> infoList = ruleBLL.ListAbsenceRules(empTypeForQuery);
            RadGrid1.DataSource = infoList;
            RadGrid1.DataBind();
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "DeleteAll")
            {
                GridItem[] items = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem);
                if (items.Length > 0)
                {
                    ((GridCommandItem)items[0]).FireCommandEvent("DeleteSelected", "");
                }
                else
                {
                    RadGrid1.Items[0].FireCommandEvent("DeleteSelected", "");
                }
            }
            else if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                ruleBLL.Delete(seq, Convert.ToInt32(Session["UserID"]));
            } 
                LoadDataGrid(""); 
        }

        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("PS_AbsenceRules.aspx" +
                                   (radCmbEmpType.SelectedValue != "0" ?
                                                                           "?empTypeSeq=" + radCmbEmpType.SelectedValue
                                        : ""));
        }

        protected void EmpTypeClick(object sender, CommandEventArgs e)
        {
            var empTypeSeqTemp = Convert.ToString(e.CommandArgument);
            int empTypeSeq;
            Int32.TryParse(empTypeSeqTemp, out empTypeSeq);
            if(empTypeSeq == 0) return;
             
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "onclick"
                , "popupWindow1(" + empTypeSeq + ");", true);
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridGroupHeaderItem) 
            { 
                GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item; 
                DataRowView groupDataRow = (DataRowView)e.Item.DataItem; 
                //item.Cells[0].Controls.Clear(); 
                (item.Cells[0].Controls[0] as Button).Enabled = false; 
            } 
        }
    }
}
